# 7.11-1
# 提取ip相关信息
# * 前置知识：json、requests
# * 接口：https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=要查询的ip地址
# * 给一个ip地址，可以通过该接口获取到省份和运营商
# * 实现一个ip查询功能
# * 用户输入一个ip地址，将查询到的结果显示给用户
# * 并将结果用以下格式写入到文件：ip,国家,省份,运营商

import requests
# 从键盘获取IP地址
ip_address = input("请输入IP地址: ")
# 构建API接口URL
api_url = "https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=" + ip_address
# 发送GET请求获取IP信息
response = requests.get(api_url)
data = response.json()
# 提取IP相关信息
ip = data['data']['ip']
country = data['data']['country']
region = data['data']['region']
isp = data['data']['isp']
# 结果
result = f"{ip},{country},{region},{isp}"
# 将结果写入文件
with open("提取ip相关信息.txt", "w", encoding="utf-8") as file:
    file.write(result)
print("查询结果已写入文件：提取ip相关信息.txt")
